/**
 * 
 */
package com.wolfpire.proms.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wolfpire.proms.model.FeedBack;
import com.wolfpire.proms.model.FeedBackAudit;
import com.wolfpire.proms.model.Task;
import com.wolfpire.proms.model.TaskPlan;
import com.wolfpire.proms.service.FeedBackService;
import com.wolfpire.proms.service.TaskPlanService;
import com.wolfpire.proms.service.TaskService;
import com.wolfpire.system.common.AuthorityUtils;
import com.wolfpire.system.common.EasyuiPage;
import com.wolfpire.system.common.EasyuiPagination;
import com.wolfpire.system.common.Page;
import com.wolfpire.system.common.Result;
import com.wolfpire.system.common.springmvc.PrincipalArgument;
import com.wolfpire.system.model.Authority;
import com.wolfpire.system.model.User;

/**
 * 任务反馈控制器
 * @author lihd
 *
 */

@Controller
@RequestMapping("/feedback")
public class FeedBackController {
	
	@Resource private FeedBackService feedBackService;
	
	@Resource private TaskService taskService;
	
	@Resource private TaskPlanService taskPlanService;
	
	@RequestMapping("/list")
	public ModelAndView list() {
		return new ModelAndView("proms/feedback/feedback_list");
	}
	
	@ResponseBody
	@RequestMapping("/queryList")
	public EasyuiPagination<FeedBack> queryList(EasyuiPage<FeedBack>page, FeedBack filterFeedBack) {
		Page<FeedBack> feedBacks = this.feedBackService.setPageDataList(page, filterFeedBack);
		return new EasyuiPagination<FeedBack>(feedBacks);
	}
	
	@RequestMapping(value = "/add")
	public ModelAndView add(HttpSession session, FeedBack feedBack) {
		ModelAndView mv = new ModelAndView("proms/feedback/feedback_add");
		if (null != feedBack.getTaskId()) {
			Task task = this.taskService.getProjectIndexInfoTask(feedBack.getTaskId());
			feedBack.setTask(task);
		}
		if(null != feedBack.getPlanId()){
			TaskPlan taskPlan = this.taskPlanService.get(feedBack.getPlanId());
			feedBack.setPreAmount(taskPlan.getPlanAmount());
			feedBack.setName(taskPlan.getName());
			feedBack.setTaskPlan(taskPlan);
		}
		mv.getModelMap().addAttribute(feedBack);
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/save")
	public Result save(@PrincipalArgument User user, FeedBack feedBack) {
		try {
			this.feedBackService.saveOrUpdate(feedBack, user);
			this.feedBackService.taskCallback(feedBack);
			return new Result(true, "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "保存失败");
		}
	}
	
	@RequestMapping("/edit")
	public ModelAndView edit(Long id) {
		ModelAndView mv = new ModelAndView("proms/feedback/feedback_edit");
		if (null != id) {
			FeedBack feedBack = this.feedBackService.getFeedBackFullInfo(id);
			mv.addObject(feedBack);
		}
		return mv;
	}
	
	@RequestMapping("/detail")
	public ModelAndView detail(Long id) {
		ModelAndView mv = new ModelAndView("proms/feedback/feedback_detail");
		try {
			if (null != id) {
				FeedBack feedBack = this.feedBackService.getFeedBackFullInfo(id);
				mv.addObject(feedBack);
			}
			return mv;
		} catch (Exception e) {
			e.printStackTrace();
			return new ModelAndView("/proms/common/error");
		}
	}
	
	@ResponseBody
	@RequestMapping("/delete")
	public Result delete(@RequestParam(value="id")Long id) {
		try {
			this.feedBackService.del(id);
			return new Result(true, "删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "删除失败");
		}
	}
	
	@RequestMapping("/audit")
	public ModelAndView audit(Long id) {
		ModelAndView mv = new ModelAndView("proms/feedback/feedback_audit");
		if (null != id) {
			FeedBack feedBack = this.feedBackService.getFeedBackFullInfo(id);
			mv.addObject(feedBack);
		}
		return mv;
	}
	
	@ResponseBody
	@RequestMapping("/saveAudit")
	public Result saveAudit(@PrincipalArgument User user, FeedBackAudit feedBackAudit) {
		try {
			feedBackAudit.setCreateUserId(user.getId());
			feedBackAudit.setCreateUserName(user.getNickName());
			this.feedBackService.saveAudit(feedBackAudit);
			return new Result(true, "保存成功");
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "保存失败");
		}
	}
	
	@RequestMapping("/auditList")
	public ModelAndView auditList(@PrincipalArgument User user, @RequestParam Long menuId) {
		//获取对应菜单的操作权限
//		List<Authority> operAuthorities = getAperAuthorities(user, menuId);
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		return new ModelAndView("proms/feedback/feedback_auditList", "operAuthorities", operAuthorities);
	}
	
	@ResponseBody
	@RequestMapping("/queryAuditList")
	public EasyuiPagination<FeedBack> queryAuditList(EasyuiPage<FeedBack>page, FeedBack filterFeedBack) {
		Page<FeedBack> feedBacks = this.feedBackService.setPageDataAuditList(page, filterFeedBack);
		return new EasyuiPagination<FeedBack>(feedBacks);
	}
	
	@RequestMapping("/myList")
	public ModelAndView myList(@PrincipalArgument User user, @RequestParam Long menuId) {
//		List<Authority> operAuthorities = getAperAuthorities(user, menuId);
		List<Authority> operAuthorities = AuthorityUtils.getAperAuthorities(user, menuId);
		return new ModelAndView("proms/feedback/feedback_myList", "operAuthorities", operAuthorities);
	}
	
	@ResponseBody
	@RequestMapping("/queryMyList")
	public EasyuiPagination<FeedBack> queryMyList(@PrincipalArgument User user, EasyuiPage<FeedBack>page, FeedBack filterFeedBack) {
		filterFeedBack.setCreateUserId(user.getId());
		Page<FeedBack> feedBacks = this.feedBackService.setPageDataList(page, filterFeedBack);
		return new EasyuiPagination<FeedBack>(feedBacks);
	}
}
